<template>
  <div class="v-editor-adornment g-jc-sb g-pd-l-8">
    <div class="g-one-line content">
      {{ valueData }}
    </div>
    <div
      v-if="selected && !row.readonly && !isModelDisabled && !tableElementDisabled"
      class="g-pd-r-4 img-btn"
    >
      <img
        src="statics/icons/app/iconmore.png"
        class="g-pointer"
        @click="handleClick"
      />
    </div>
  </div>
</template>
<script lang="ts" setup>
import { PropertyRow } from "../../types";
import { Ref, computed, inject } from "vue";
import { PropertyKey, RelationCriteriaType } from "@/constants/Enums";
// import { propertyUtil } from '@/modules/model/comps/station/sider/attribute/PropertyUtil';
import { modelService } from "@/modules/model/service";
import { relationCriteriaWindow } from "@/modules/model/modals/relationCriteria";
import { parseRC2Form } from "@/util/relationCriteria";
import { locale } from "@/util/locale";
import { MetaclassType } from "@/constants/MetaclassType";

const isModelDisabled = inject("isModelDisabled") as Ref<boolean>;

/**
 * 模型单选-树选择弹窗
 */
const props = defineProps<{
  row: PropertyRow;
  selected?: boolean;
  tableElementDisabled?: boolean;
}>();

const valueData = computed(() => {
  if (!props.row.value) return "";
  return parseRC2Form(props.row.value[0] as string);
});

const handleClick = async () => {
  const projectId = app.activeProject?.id;
  if (!projectId) return;
  const row = props.row;
  const legendItemSpecificElements = (await modelService.getPropertyValueById(
    projectId,
    row.modelId,
    PropertyKey.LegendItem_specificElements
  )) as string[];
  if (legendItemSpecificElements?.length) {
    const res = await app.$confirm.popup({
      title: "提示",
      subTitle: locale.t("该图例项已设置指定元素"),
      content: locale.t("设置匹配条件将覆盖已设置的指定元素，是否继续？"),
      status: "warn",
      cancelText: locale.t("button.no"),
      okText: locale.t("button.yes")
    });
    if (res.type == "cancel") return;
  }
  row.value = row.value || [];
  // (row.value as any[]).push({ operationName: "属性判断111", conditionType: "TypeCondition", typeIds: ["Block"] });
  // (row.value as any[]).push({ operationName: "连接器匹配", conditionType: "ConnectorCondition", portId: 'port' });

  relationCriteriaWindow
    .popup({
      projectId: app.activeProject!.id,
      attrTip: "LegendItemCondition",
      title: "设置匹配条件",
      type: RelationCriteriaType.LEGEND,
      modelId: row.modelId,
      validType: MetaclassType.Element
    })
    .then(async res => {
      if (!res.success) return;
      // propertyUtil.updatePropertyAndFresh(row, res.relationCriteria ? [JSON.parse(res.relationCriteria)] : []);
    });
};
</script>
<style lang="scss" scoped>
.v-editor-adornment {
  height: 100%;
  .content {
    text-overflow: ellipsis;
    white-space: nowrap;
    display: flex;
    align-items: center;
    > span {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }
  > .img-btn {
    display: flex;
    align-items: center;
  }
}
</style>
